package com.sunday.shoufen.ui.mine;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.ClearEditText;
import com.sunday.shoufen.R;
import com.sunday.shoufen.base.BaseApp;
import com.sunday.shoufen.http.ApiClient;
import com.sunday.shoufen.ui.widgets.HyRechangeRecordActivity;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Author by Damon,  on 2018/1/19.
 */

public class HyRechangeAcitvity extends BaseActivity {


    @Bind(R.id.left_btn)
    ImageView leftBtn;
    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.rightTxt)
    TextView rightTxt;
    @Bind(R.id.right_btn)
    ImageView rightBtn;
    @Bind(R.id.right_btn1)
    ImageView rightBtn1;
    @Bind(R.id.common_header)
    RelativeLayout commonHeader;
    @Bind(R.id.edit_profit)
    ClearEditText editProfit;
    @Bind(R.id.available_profit)
    TextView availableProfit;
    @Bind(R.id.btn_next)
    TextView btnNext;

    private String availbleHyScore;
    private long memberId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hy_rechange);
        ButterKnife.bind(this);
        titleView.setText("怀亚积分回购");
        memberId = BaseApp.getInstance().getMember().getId();
        rightTxt.setText("怀亚积分记录");
        availbleHyScore = getIntent().getStringExtra("availbleHyScore");
        availableProfit.setText("可回购怀亚积分数目" + availbleHyScore);

    }

    @OnClick(R.id.rightTxt)
    void getRecord(){
        intent=new Intent(mContext, HyRechangeRecordActivity.class);
        startActivity(intent);
    }




    private String rechangeScore;

    @OnClick(R.id.btn_next)
    void rechange() {
        rechangeScore = editProfit.getText().toString();
        if (TextUtils.isEmpty(rechangeScore)) {
            ToastUtils.showToast(mContext, "请填写回购数量");
            return;
        }
        if (Double.parseDouble(availbleHyScore) <= 0) {
            ToastUtils.showToast(mContext, "暂无可回购怀亚积分");
            return;
        }
        if (Double.parseDouble(rechangeScore) > Double.parseDouble(availbleHyScore)) {
            ToastUtils.showToast(mContext, "超出最大怀亚积分数量！");
            return;
        }

        showLoadingDialog(0);
        Call<ResultDO> call = ApiClient.getApiAdapter().huaiYaScoreBack(memberId, rechangeScore);
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                dissMissDialog();
                if (isFinish || response.body() == null) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    ToastUtils.showToast(mContext, "兑换成功");
                    finish();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });


    }
}
